const { Service } = require('egg');
const ERROR_CODE = require('../errorCode');
const {
  ExperimentCourse,
  Student,
  ExperimentScore,
} = require('../db/model');

class ExperimentScoreService extends Service {
  async getAllExperimentScore() {
    await ExperimentCourse.hasMany(ExperimentScore);
    await ExperimentScore.belongsTo(ExperimentCourse, {
      foreignKey: 'ecId',
    });
    await Student.hasMany(ExperimentScore);
    await ExperimentScore.belongsTo(Student, {
      foreignKey: 'sId',
    });
    const res = await ExperimentScore.findAll({
      attributes: { exclude: ['id', 'ExperimentCourseEcId', 'StudentSId'] },
      include: [ Student, ExperimentCourse ],
    }).catch(err => {
      console.log(`ExperimentScore 表查找失败: ${err}`);
    });
    if (res === undefined) {
      console.log(`ExperimentScore 表查找失败: ${ERROR_CODE.DATABASE_ERROR}`);
      return ERROR_CODE.DATABASE_ERROR;
    }
    return res;
  }

  async getExperimentScore(experimentScore) {
    if (!(experimentScore?.sId && experimentScore?.ecId)) {
      console.log(`ExperimentScore 表查找失败: ${ERROR_CODE.PARAMETERS_MISSING}\n参数: ${experimentScore}`, experimentScore);
      return ERROR_CODE.PARAMETERS_MISSING;
    }
    await ExperimentCourse.hasMany(ExperimentScore);
    await ExperimentScore.belongsTo(ExperimentCourse, {
      foreignKey: 'ecId',
    });
    await Student.hasMany(ExperimentScore);
    await ExperimentScore.belongsTo(Student, {
      foreignKey: 'sId',
    });
    const res = await ExperimentScore.findOne({
      where: {
        sId: experimentScore?.sId || -1,
        ecId: experimentScore?.ecId || -1,
      },
      attributes: { exclude: ['id', 'ExperimentCourseEcId', 'StudentSId'] },
      include: [ Student, ExperimentCourse ],
    }).catch(err => {
      console.log(`ExperimentScore 表查找失败: ${err}\n参数: ${experimentScore}`, experimentScore);
    });
    if (res === undefined) {
      console.log(`ExperimentScore 表查找失败: ${ERROR_CODE.DATABASE_ERROR}\n参数: ${experimentScore}`, experimentScore);
      return ERROR_CODE.DATABASE_ERROR;
    }
    if (res === null) {
      console.log(`ExperimentScore 表查找失败: ${ERROR_CODE.PRIMARY_KEY_NOT_EXIST}\n参数: ${experimentScore}`, experimentScore);
      return ERROR_CODE.PRIMARY_KEY_NOT_EXIST;
    }
    return res;
  }

  async appendExperimentScore(experimentScore) {
    let res = await this.getExperimentScore(experimentScore);
    if (res instanceof Object) {
      res = ERROR_CODE.PRIMARY_KEY_EXIST;
    }
    if (res !== ERROR_CODE.PRIMARY_KEY_NOT_EXIST) {
      console.log(`ExperimentScore 表新增失败: ${res}\n参数: ${experimentScore}`, experimentScore);
      return res;
    }
    res = await ExperimentScore.create(experimentScore).catch(err => {
      console.log(`ExperimentScore 表新增失败: ${err}\n参数: ${experimentScore}`, experimentScore);
    });
    if (res === undefined) {
      console.log(`ExperimentScore 表新增失败: ${ERROR_CODE.DATABASE_ERROR}\n参数: ${experimentScore}`, experimentScore);
      return ERROR_CODE.DATABASE_ERROR;
    }
    return res;
  }

  async updateExperimentScore(experimentScore) {
    let res = await this.getExperimentScore(experimentScore);
    if (Object.values(ERROR_CODE).includes(res)) {
      console.log(`ExperimentScore 表更新失败: ${res}\n参数: ${experimentScore}`, experimentScore);
      return res;
    }
    res = await ExperimentScore.update(experimentScore, {
      where: {
        sId: experimentScore?.sId || -1,
        ecId: experimentScore?.ecId || -1,
      },
    }).catch(err => {
      console.log(`ExperimentScore 表更新失败: ${err}\n参数: ${experimentScore}`, experimentScore);
    });
    if (res === undefined) {
      console.log(`ExperimentScore 表更新失败: ${ERROR_CODE.DATABASE_ERROR}\n参数: ${experimentScore}`, experimentScore);
      return ERROR_CODE.DATABASE_ERROR;
    }
    return res;
  }

  async destroyExperimentScore(experimentScore) {
    let res = await this.getExperimentScore(experimentScore);
    if (Object.values(ERROR_CODE).includes(res)) {
      console.log(`ExperimentScore 表删除失败: ${res}\n参数: ${experimentScore}`, experimentScore);
      return res;
    }
    res = await ExperimentScore.destroy({
      where: {
        sId: experimentScore?.sId || -1,
        ecId: experimentScore?.ecId || -1,
      },
    }).catch(err => {
      console.log(`ExperimentScore 表删除失败: ${err}\n参数: ${experimentScore}`, experimentScore);
    });
    if (res === undefined) {
      console.log(`ExperimentScore 表删除失败: ${ERROR_CODE.DATABASE_ERROR}\n参数: ${experimentScore}`, experimentScore);
      return ERROR_CODE.DATABASE_ERROR;
    }
    return res;
  }
}

module.exports = ExperimentScoreService;
